#!/usr/bin/perl
#index.cgi

require "./mdmgroups-lib.pl";

&header($text{'index_title'}, "", "index", 0, 1);

&foreign_require("mdmmod","mdmmod-lib.pl");
local $mdmmod=&foreign_call("mdmmod","get_mdm_mod");
if ($mdmmod ne 'server'){
	print "$text{'deny'}\n";
	&footer("/",$text{'index'});
	exit;
}

if (!(-f $config{'groupfile'})){
	`mkdir -p $config{'groupdir'}`;
	`touch $config{'groupfile'}`;
}

&refresh_grpinfo();
local $infoaddr=&get_groupinfo();
local %groupinfo=%$infoaddr;
local @grouporder = split (/,/,$groupinfo{'[group]'});
local @group= sort {$a cmp $b } @grouporder;

print "<form action=index.cgi method=post onsubmit='return onsubmit_form();'>\n";
print "<table width=100% class=mode2><tr>\n";
print "<td><div class=main><table class=mode1 border=0 bordercolor=#000000 cellspacing=0 cellpadding=1 width=100%>\n";
print "<tr class=title1> <td><b>&nbsp;&nbsp;&nbsp;&nbsp;$text{'groupname'}</b></td> ","<td><b>$text{'userlist'}</b></td></tr>\n";
local $mod=mode2;
if (@group){
	print "<input type=hidden name=nulldev value='no'>\n";	
}else{
	print "<input type=hidden name=nulldev value='yes'>\n";	
}
for($i=0;$i<@group;$i++){
	$groupname=$group[$i];
	local $username=&display_3_members($groupinfo{"$groupname"});
	if ($mod eq "mode1"){
		$mod=mode2;
	}else{
		$mod=mode1;
	}
	
	printf "<tr class=$mod><td><input type=radio %s value=$groupname name=groupname>$groupname</td><td>$username</td></tr>\n",($i==0)?'checked':'';
}
print "</table></div></td>\n";
print "<td valign=top width=120>";
print "<table class=mode1 cellpadding=4 width=100%><tr>\n";
print "<tr><td><input type=submit name=btcreate class=button value=\'$text{'butn_create'}\' onclick='return on_click(1, \" \");'></td></tr>\n";
print "<tr><td><input type=submit name=btedit class=button value=\'$text{'butn_edit'}\' onclick='return on_click(2, \" \", \"$text{'no_dev'}\");'></td></tr>\n";
print "<tr><td><input type=submit name=btdelete class=button value=\'$text{'butn_delete'}\' onclick='return on_click(3, \"$text{'sure_del'}\", \"$text{'no_dev'}\");'></td></tr>\n";
print "</table></td></tr></table>";
print <<EOF;
<script>
function onsubmit_form()
{
        document.forms[0].btcreate.disabled = true;
        document.forms[0].btedit.disabled = true;
        document.forms[0].btdelete.disabled = true;
}

function on_click(num, confirm_msg, alert_msg)
{
	if (num == 1){
		document.forms[0].action = "create_group.cgi";
		return;
	}
	
	if(num == 2){
		if (document.forms[0].nulldev.value == "yes"){
			alert (alert_msg);
			return false;	
		}
		document.forms[0].action = "edit_group.cgi";
		return;
	}
	
	if (num == 3){
		if (document.forms[0].nulldev.value == "yes"){
			alert (alert_msg);
			return false;	
		}		
		if(!confirm(confirm_msg)){
	               	return false;
        		}
		document.forms[0].action = "delete_group.cgi";
		return;
	}
}
</script>
EOF


&footer("/",$text{'index'});

1;